import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store/index'

Vue.use(VueRouter)
//定义routes路由的集合，数组类型
const routes = [
    //单个路由均为对象类型，path代表的是路径，component代表组件
    {
        path: '/',
        component: () => import('@/views/home'),
    },
    {
        path: '/home',
        redirect: '/'
    },
    {
        path: '/login',
        component: () => import('@/views/login')
    },
    {
        path: '/article',
        name: 'article',
        component: () => import('@/views/article')
    },
    {
        path: '/WangEditor',
        component: () => import('@/components/tools/WangEditor')
    },
    {
        path: '/articleType',
        component: () => import('@/views/articleType')
    },
    {
        path: '/domain',
        component: () => import('@/views/domain'),
        children: [
            {
                path: '/domain/myPost',
                component: () => import('@/components/domain/MyPost')
            },
        ]
    }
]


//实例化VueRouter并将routes添加进去
const router = new VueRouter({
    //ES6简写，等于routes：routes
    routes
});

//利用vue-router提供的钩子函数beforeEach()对路由进行判断
router.beforeEach((to, from, next) => {

    //当vuex刷新重置时,向后端发送请求，更新vuex的登录数据


    //动态更新vuex中登录信息的状态
    if(!window.localStorage.getItem("token")) {
        store.commit('setIsLogin',false);
    }
    else {
        store.commit('setIsLogin',true);
    }
    next();
})


//抛出这个这个实例对象方便外部读取以及访问
export default router